package com.zxl.yjb.jpush;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;

import com.zxl.yjb.activity.MainActivity;
import com.zxl.yjb.activity.home.order.SosDetailActivity;
import com.zxl.yjb.activity.im.DialogActivity;
import com.zxl.yjb.app.AppManager;
import com.zxl.yjb.app.Const;
import com.zxl.yjb.bean.LoginSucessBean;
import com.zxl.yjb.bean.MessageEvent;
import com.zxl.yjb.utils.NLog;
import com.zxl.yjb.utils.SaveBeanUtil;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONException;
import org.json.JSONObject;

import cn.jpush.android.api.JPushInterface;

/**
 * 自定义接收器
 * <p>
 * 如果不定义这个 Receiver，则：
 * 1) 默认用户会打开主界面
 * 2) 接收不到自定义消息
 */
public class JpushReceiver extends BroadcastReceiver {
    private static final String TAG = "JIGUANG-Example";

    @Override
    public void onReceive(Context context, Intent intent) {
        try {
            Bundle bundle = intent.getExtras();
//            NLog.d(TAG, "[JpushReceiver] onReceive - " + intent.getAction() + ", extras: " + printBundle(bundle));

            if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
                String regId = bundle.getString(JPushInterface.EXTRA_REGISTRATION_ID);
                NLog.d(TAG, "[JpushReceiver] 接收Registration Id : " + regId);
                //send the Registration Id to your server...

            } else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
                NLog.d(TAG, "[JpushReceiver] 接收到推送下来的自定义消息: " + bundle.getString(JPushInterface.EXTRA_MESSAGE));
                processCustomMessage(context, bundle);

            } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
                NLog.d(TAG, "[JpushReceiver] 接收到推送下来的通知");
                int notifactionId = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID);
                NLog.d(TAG, "[JpushReceiver] 接收到推送下来的通知的ID: " + notifactionId);
                processCustomMessage(context, bundle);
            } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
                NLog.d(TAG, "[JpushReceiver] 用户点击打开了通知");

                LoginSucessBean loginSucessBean = (LoginSucessBean) SaveBeanUtil.getBean(context,Const.VIPINFO);
                //打开自定义的Activity
                if (loginSucessBean!=null)
                {
                    Intent i = new Intent(context, MainActivity.class);
                    i.putExtras(bundle);
                    //i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    context.startActivity(i);
                }


            } else if (JPushInterface.ACTION_RICHPUSH_CALLBACK.equals(intent.getAction())) {
                NLog.d(TAG, "[JpushReceiver] 用户收到到RICH PUSH CALLBACK: " + bundle.getString(JPushInterface.EXTRA_EXTRA));
                //在这里根据 JPushInterface.EXTRA_EXTRA 的内容处理代码，比如打开新的Activity， 打开一个网页等..

            } else if (JPushInterface.ACTION_CONNECTION_CHANGE.equals(intent.getAction())) {
                boolean connected = intent.getBooleanExtra(JPushInterface.EXTRA_CONNECTION_CHANGE, false);
                NLog.w(TAG, "[JpushReceiver]" + intent.getAction() + " connected state change to " + connected);
            } else {
                NLog.d(TAG, "[JpushReceiver] Unhandled intent - " + intent.getAction());
            }
        } catch (Exception e) {

        }

    }


    //send msg to MainActivity
    private void processCustomMessage(Context context, Bundle bundle) {
//        String message = bundle.getString(JPushInterface.EXTRA_ALERT);
//        String message = bundle.getString(JPushInterface.EXTRA_MESSAGE);
        String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
        if (TextUtils.isEmpty(extras)) {
            return;
        }
        JSONObject jsonObject = null;
        String type = "";
        String order_sn = "";
        String charge_id = "";
        String amount = "";
        try {
            jsonObject = new JSONObject(extras);
            type = jsonObject.optString("type");
            order_sn = jsonObject.optString("order_sn");
            charge_id = jsonObject.optString("charge_id");
            amount = jsonObject.optString("amount");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        if ("1".equals(type)&&!TextUtils.isEmpty(order_sn)) {
            Intent intent = new Intent();
            Activity activity = AppManager.getInstance().findActivity(MainActivity.class);
            if (activity == null)
                intent.setClass(context, MainActivity.class);
            else
                intent.setClass(context, DialogActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//            intent.putExtra(Const.KEY_EXTRAS, extras);
            intent.putExtra("id", order_sn);
            intent.putExtra("charge_id", charge_id);
            intent.putExtra("amount", amount);
            intent.putExtra("type", type);
            context.startActivity(intent);
        }
        else if ("sos".equals(type))
        {
            /*Intent intent = new Intent();
            Activity activity = AppManager.getInstance().findActivity(MainActivity.class);
            if (activity == null)
                intent.setClass(context, MainActivity.class);
            else
                intent.setClass(context, SosDetailActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//            intent.putExtra(Const.KEY_EXTRAS, extras);
            intent.putExtra("id", order_sn);
            intent.putExtra("charge_id", charge_id);
            intent.putExtra("amount", amount);
            intent.putExtra("type", type);
            context.startActivity(intent);*/
            EventBus.getDefault().post(new MessageEvent("new_sos"));
        }


    }

}
